home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / xlib03.zip / XBMTOOLS.ASM < prev    next >
Assembly Source File  |  1993-04-05  |  6KB  |  201 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XBMTOOLS
  3. ;
  4. ; Bitmap conversion / manipulation tools
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ; ****** Aeronautical Research Laboratory              ****************
  14. ; ****** Defence Science and Technology Organisation   ****************
  15. ; ****** Australia                                     ****************
  16. ;
  17. ; egg@dstos3.dsto.gov.au
  18. ; teg@bart.dsto.gov.au
  19. ;-----------------------------------------------------------------------
  20. COMMENT $
  21.  
  22.   This module implements a set of functions to manipulate both planar
  23.   bitmaps and linear bitmaps (as used by x_compile_bitmap):
  24.  
  25.   PLANAR BITMAPS
  26.  
  27.   Planar bitmaps as used by these functions have the following structure:
  28.  
  29.   BYTE 0                 The bitmap width in bytes (4 pixel groups) range 1..255
  30.   BYTE 1                 The bitmap height in rows range 1..255
  31.   BYTE 2..n1             The plane 0 pixels width*height bytes
  32.   BYTE n1..n2            The plane 1 pixels width*height bytes
  33.   BYTE n2..n3            The plane 2 pixels width*height bytes
  34.   BYTE n3..n4            The plane 3 pixels width*height bytes
  35.  
  36.   LINEAR BITMAPS
  37.  
  38.   Linear bitmaps have the following structure:
  39.  
  40.   BYTE 0                 The bitmap width in pixels  range 1..255
  41.   BYTE 1                 The bitmap height in rows   range 1..255
  42.   BYTE 2..n              The width*height bytes of the bitmap
  43.  
  44.  
  45.  
  46. $
  47.  
  48. LOCALS
  49. .286
  50.  
  51. include model.inc
  52. include xbmtools.inc
  53.  
  54.     .code
  55.  
  56.  
  57.  
  58. ;-----------------------------------------------------------------------
  59. ; x_pbm_to_bm
  60. ;
  61. ; This function converts a bitmap in the planar format to the linear format
  62. ; as used by x_compile_bitmap.
  63. ;
  64. ; WARNING: the source and destination bitmaps must be pre - allocated
  65. ;
  66. ; NOTE: This function can only convert planar bitmaps that are suitable.
  67. ;       If the source planar bitmap's width (per plane) is >= 256/4
  68. ;       it cannot be converted. In this situation an error code
  69. ;       BM_WIDTH_ERROR. On successful conversion 0 is returned.
  70. ;
  71. ; C callable as:
  72. ;    int x_pbm_to_bm(char far * source_pbm, char far * dest_bm);
  73. ;
  74. ; Written By Themie Gouthas
  75.  
  76. proc _x_pbm_to_bm
  77. ARG   src_pbm:dword,dest_bm:dword
  78.     push bp                  ; Preserve caller's stack frame
  79.     mov  bp,sp
  80.     push ds
  81.     push di
  82.     push si
  83.  
  84.     les  di,[dest_bm]     ; es:di -> destination bitmap
  85.     lds  si,[src_pbm]     ; ds:si -> source planar bitmap
  86.     lodsb                 ; load AL with source pbm pixel width per plane
  87.     mov  bl,al            ; save in CL
  88.         xor  ah,ah            ; convert to word
  89.     shl  ax,2             ; mult by 4 giving source image width
  90.     cmp  ax,255           ; if the result > 255 then we have exceeded
  91.     jl   @@WidthError     ; the max width of linear bm.
  92.  
  93.     stosb                 ; write do dest_bm
  94.  
  95.  
  96.     lodsb                 ; tranfer source pbm height in pixels to
  97.     stosb              ;  dest_bm
  98.  
  99.     xor  ah,ah            ; convert to word
  100.     mul  bl               ; AX = AX * BL ie. total no. pixels per plane
  101.     mov  dx,di            ; save DI, the pointer to the destination bm
  102.     mov  bl,3             ; set plane loop counter (BL)
  103.  
  104. @@PlaneLoop:
  105.     mov  cx,ax            ; set CX to total number of pixels per plane
  106.  
  107. @@PixelLoop:
  108.     movsb                 ; transfer pixel
  109.     add  di,3             ; increment destination to compensate for plane
  110.     loop @@PixelLoop
  111.  
  112.     inc  dx               ; increment original di for next pixel plane
  113.     mov  di,dx            ; and restore di from incremented original
  114.     dec  bl               ; decrement plane counter
  115.     jns  @@PlaneLoop      ; loop if more planes left
  116.     xor  ax,ax
  117.     jmp  short @@Done
  118. @@WidthError:
  119.     mov  ax,1
  120.     jmp  short @@Done
  121.  
  122. @@Done:
  123.     pop  si
  124.     pop  di
  125.     pop  ds
  126.     pop  bp                  ;restore caller's stack frame
  127.     ret
  128. _x_pbm_to_bm endp
  129.  
  130.  
  131. ;-----------------------------------------------------------------------
  132. ; x_bm_to_pbm
  133. ;
  134. ; This function converts a bitmap in the linear format as used by
  135. ; x_compile_bitmap to the planar formap.
  136. ;
  137. ; WARNING: the source and destination bitmaps must be pre - allocated
  138. ;
  139. ; NOTE: This function can only convert linear bitmaps that are suitable.
  140. ;       If the source linear bitmap's width is not a multiple of 4
  141. ;       it cannot be converted. In this situation an error code
  142. ;       BM_WIDTH_ERROR. On successful conversion 0 is returned.
  143. ;
  144. ;
  145. ; C callable as:
  146. ;    int x_bm_to_pbm(char far * source_pbm, char far * dest_bm);
  147. ;
  148. ; Written By Themie Gouthas
  149.  
  150. proc _x_bm_to_pbm
  151. ARG src_bm:dword,dest_pbm:dword
  152.     push bp                  ; Preserve caller's stack frame
  153.     mov  bp,sp
  154.     push ds
  155.     push di
  156.     push si
  157.  
  158.     les  di,[dest_pbm]       ; es:di -> destination planar bitmap
  159.     lds  si,[src_bm]         ; ds:si -> source bitmap
  160.     lodsb                    ; load AX with source bitmap width
  161.     test al,03h              ; Check that width is a multiple of 4
  162.     jnz  @@WidthIncompatible
  163.     shr  al,2                ; divide by 4 giving width of plane
  164.     stosb                    ; store destination planar bitmap width
  165.     mov  bl,al               ;  and copy to bl
  166.     lodsb
  167.     stosb             ; Transfer source bitmap height to dest pbm
  168.     xor  ah,ah               ; Conver height to word
  169.     mul  bl                  ; calculate the total no. of pixels / plane
  170.     mov  dx,si               ; save source offset
  171.     mov  bl,3
  172.  
  173. @@PlaneLoop:
  174.     mov  cx,ax            ; set CX to total number of pixels per plane
  175.  
  176. @@PixelLoop:
  177.     movsb                 ; transfer pixel
  178.     add  si,3             ; increment src offset to compensate for plane
  179.     loop @@PixelLoop
  180.  
  181.     inc  dx               ; increment original si for next pixel plane
  182.     mov  si,dx            ; and restore si from incremented original
  183.     dec  bl               ; decrement plane counter
  184.     jns  @@PlaneLoop      ; loop if more planes left
  185.     xor  ax,ax
  186.     jmp  short @@Done
  187. @@WidthIncompatible:
  188.     mov  ax,1
  189.     jmp  short @@Done
  190. @@Done:
  191.     pop  si
  192.     pop  di
  193.     pop  ds
  194.     pop  bp                  ;restore caller's stack frame
  195.     ret
  196. _x_bm_to_pbm endp
  197.  
  198.  
  199.     end
  200.  
  201.